<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'core/app/AppAjax.php';


class KBClientView_contact extends KBClientView_common
{

	var $found_entries = array();

	
	function &execute(&$manager) {
		
		$this->home_link = true;
		$this->meta_title = $this->msg['contact_us_msg'];
		$this->category_nav_generate = false; // not to generate categories in navigation line
		
		$data = $this->getTop($manager, $this->msg['contact_us_msg'], false);
		$data .= $this->getForm($manager);
		
		return $data;
	}
	

	function getForm(&$manager) {
		
		$tpl = &new tplTemplatez($this->template_dir . 'contact_form.html');
		
		$simple_arr = &$this->stripVars($manager->categories);
		$select_range = &$manager->getCategorySelectRange($simple_arr);
		
		// categories
		$select = new FormSelect();
		$select->setFormMethod($_POST);
		$select->select_tag = false;
		//$select->setSelectWidth(200);
		$select->setSelectName('category_id');
		$select->setRange($select_range, array(0=>'____'));
		
		$tpl->tplAssign('category_select', $select->select($this->category_id));
		
		if(APP_DEMO_MODE) {
			$msg = BoxMsg::factory('hint');
			$msg->img = false;
			$msg->setMsgs('', 'To test "Quick Responce" type "What is freeware?"');
			$tpl->tplAssign('demo_responce_msg', $msg->get());
		}
		
		$tpl->tplAssign('user_msg', $this->getErrors());
		$tpl->tplAssign('action_link', $this->_getLink('all'));
		$tpl->tplAssign('cancel_link', $this->_getLink('index', $this->category_id));
		
		
		// attachments
		$num = $manager->getSetting('contact_attachment');
		for($i=1; $i<=$num; $i++) {
			$a['num'] = $i;
			$a['attachment_msg'] = $this->msg['attachment_msg'];
			$tpl->tplParse($a, 'attachment');
		}
		
			
		//xajax
		ini_set('arg_separator.output', '&amp;'); // required for IIS

		$this->ajax_manager = &$manager;
		$xajax = &new AppAjax();
		//$xajax->debugOn();
		$xajax->setRequestURI($this->_getLink('all'));
		$xajax->setCharEncoding($this->encoding);
		$xajax->decodeUTF8InputOn();
		$xajax->js_dir = $this->controller->kb_path . 'client/jscript/';
		$xajax->extra_js = sprintf('<script src="%s" type="text/javascript"></script>', 
		                           $this->controller->kb_path . 'client/jscript/xajax_js/OnKeyRequestBuffer.js');
		//$xajax->debugOn();
		//$xajax->registerFunction(array('getQuickResponce', $this, 'ajaxGetQuickResponce'));
		$xajax->registerFunction(array('requestBuffer', $this, 'ajaxGetQuickResponce'));
		$xajax_js = $xajax->getJavascript();
		$xajax->processRequests();
		
		$tpl->tplAssign('xajax_js', $xajax_js);
		
		
		if(!$manager->is_registered) {
			$tpl->tplSetNeeded('/not_registered');
		}
		
		if($this->useCaptcha($manager, 'contact')) {
			$tpl->tplSetNeeded('/captcha');
			$tpl->tplAssign('captcha_src', $this->getCaptchaSrc());
		}		
		
		$tpl->tplAssign($this->msg);
		$tpl->tplAssign($this->getFormData());	
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function validate($values, $manager) {
		 
		require_once 'eleontev/Validator.php';
		
		$v =& new Validator($values, false);
		
		// need to check if not registered
		if(!$manager->is_registered) {
			$v->requiredArray('required_msg', array('message', 'email'));
			$v->regex('email_msg', 'email', 'email');
		} else {
			$v->requiredArray('required_msg', array('message'));
		}
		
		if($this->useCaptcha($manager, 'contact')) {
			if($_SESSION['kb_captcha_'] != $values['captcha']) {
				$v->setError('captcha_text_msg', 'captcha', 'captcha');
			}
		}		
		
		return $v->getErrors();
	}
	
	
	function ajaxGetQuickResponce($str) {
	
		if(strlen($str) < 10) { 
			$objResponse = new xajaxResponse();
			return $objResponse;
		}
		
		$values['q'] = trim($str);
		$values['q'] = str_replace('"', '', $values['q']);
		$seach_view = &$this->controller->getView('search_list');
		$entries = &$seach_view->getEntryListQuickResponce($this->ajax_manager, $values);
		
		
		$objResponse = new xajaxResponse();
		$objResponse->setCharEncoding($this->encoding);
		//$objResponse->addAlert($values['q']);
		//$objResponse->addScript("document.getElementById('quick_responce').style.display = 'inline';");
		$objResponse->addAssign("quick_responce", "innerHTML", "$entries");		
	
		return $objResponse;	
	}
}
?>